﻿using Rafy;
using Rafy.ComponentModel;
using Rafy.Data;
using Rafy.Domain;
using Rafy.Domain.ORM;
using Rafy.Domain.ORM.Query;
using Rafy.Domain.Validation;
using Rafy.ManagedProperty;
using Rafy.MetaModel;
using Rafy.MetaModel.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;
using static Rafy.Domain.ORM.Query.FactoryMethods;

namespace Rafy.Book
{
    /// <summary>
    /// 语文课本关联词语
    /// </summary>
    [RootEntity]
    public partial class YuWen_BookWords : BookEntity
    {
        #region 引用属性
        public static readonly Property<int> F_WordsIdProperty = P<YuWen_BookWords>.Register(e => e.F_WordsId);
        public int F_WordsId
        {
            get { return this.GetProperty(F_WordsIdProperty); }
            set { this.SetProperty(F_WordsIdProperty, value); }
        }
        public static readonly RefEntityProperty<YuWen_Words> F_WordsProperty =
            P<YuWen_BookWords>.RegisterRef(e => e.F_Words, F_WordsIdProperty);
        /// <summary>
        /// 所属词语
        /// </summary>
        public YuWen_Words F_Words
        {
            get { return this.GetRefEntity(F_WordsProperty); }
            set { this.SetRefEntity(F_WordsProperty, value); }
        }
        #endregion

        #region 组合子属性

        #endregion

        #region 一般属性
        public static readonly Property<int> F_BookIdProperty = P<YuWen_BookWords>.Register(e => e.F_BookId);
        /// <summary>
        /// 所属课本
        /// </summary>
        public int F_BookId
        {
            get { return this.GetProperty(F_BookIdProperty); }
            set { this.SetProperty(F_BookIdProperty, value); }
        }
        public static readonly Property<int> F_CatalogIdProperty = P<YuWen_BookWords>.Register(e => e.F_CatalogId);
        /// <summary>
        /// 所属目录
        /// </summary>
        public int F_CatalogId
        {
            get { return this.GetProperty(F_CatalogIdProperty); }
            set { this.SetProperty(F_CatalogIdProperty, value); }
        }
        public static readonly Property<int> F_UserIdProperty = P<YuWen_BookWords>.Register(e => e.F_UserId);
        /// <summary>
        /// 所属用户
        /// </summary>
        public int F_UserId
        {
            get { return this.GetProperty(F_UserIdProperty); }
            set { this.SetProperty(F_UserIdProperty, value); }
        }

        public static readonly Property<bool> ShowPinyinProperty = P<YuWen_BookWords>.Register(e => e.ShowPinyin);
        /// <summary>
        /// 是否显示拼音【高年级不显示】
        /// </summary>
        public bool ShowPinyin
        {
            get { return this.GetProperty(ShowPinyinProperty); }
            set { this.SetProperty(ShowPinyinProperty, value); }
        }
        #endregion

        #region 冗余属性
        public static readonly Property<string> F_NameProperty = P<YuWen_BookWords>.Register(e => e.F_Name);
        /// <summary>
        /// 词语名称
        /// </summary>
        public string F_Name
        {
            get { return this.GetProperty(F_NameProperty); }
            set { this.SetProperty(F_NameProperty, value); }
        }
        public static readonly Property<string> F_PinYinProperty = P<YuWen_BookWords>.Register(e => e.F_PinYin);
        /// <summary>
        /// 词语拼音
        /// </summary>
        public string F_PinYin
        {
            get { return this.GetProperty(F_PinYinProperty); }
            set { this.SetProperty(F_PinYinProperty, value); }
        }
        #endregion
    }

    /// <summary>
    /// 语文课本关联词语 列表类。
    /// </summary>
    public partial class YuWen_BookWordsList : BookEntityList<YuWen_BookWords> { }

    /// <summary>
    /// 语文课本关联词语 仓库类。
    /// 负责 语文课本关联词语 类的查询、保存。
    /// </summary>
    public partial class YuWen_BookWordsRepository : BookEntityRepository
    {
        /// <summary>
        /// 单例模式，外界不可以直接构造本对象。
        /// </summary>
        protected YuWen_BookWordsRepository() { }

        [RepositoryQuery]
        public virtual YuWen_BookWordsList GetsBy(int bookId, int catalogId)
        {
            var q = this.CreateLinqQuery();
            q = q.Where(e => e.F_BookId == bookId && e.F_CatalogId == catalogId);
            return (YuWen_BookWordsList)this.QueryData(q);
        }

        [RepositoryQuery]
        public virtual YuWen_BookWords GetBy(int bookId, int cataId, string words)
        {
            var q = this.CreateLinqQuery();
            q = q.Where(e => e.F_BookId==bookId && e.F_CatalogId==cataId && e.F_Name == words);
            return (YuWen_BookWords)this.QueryData(q);
        }
    }

    /// <summary>
    /// 语文课本关联词语 配置类。
    /// 负责 语文课本关联词语 类的实体元数据的配置。
    /// </summary>
    internal class YuWen_BookWordsConfig : BookEntityConfig<YuWen_BookWords>
    {
        /// <summary>
        /// 配置实体的元数据
        /// </summary>
        protected override void ConfigMeta()
        {
            //配置实体的所有属性都映射到数据表中。
            Meta.MapTable().MapAllProperties();
            MapRefValue(YuWen_BookWords.F_NameProperty, e => e.F_Words.F_Name, ReferenceValueDataMode.Redundancy);
            MapRefValue(YuWen_BookWords.F_PinYinProperty, e => e.F_Words.F_PinYin, ReferenceValueDataMode.Redundancy);
        }
    }
}